﻿using System;
using System.Collections.Generic;

namespace VisitorPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            var woman1 = new Woman("lili", 48);
            var woman2 = new Woman("luck", 60);
            var woman3 = new Woman("cici", 70);
            var man1 = new Man("A", 180);
            var man2 = new Man("A", 178);
            var man3 = new Man("A", 160);
            var objectStructure = new ObjectStructure();
            objectStructure.Add(woman1);
            objectStructure.Add(woman2);
            objectStructure.Add(woman3);
            objectStructure.Add(man1);
            objectStructure.Add(man2);
            objectStructure.Add(man3);


            var gradeSection = new GradeSelection();
            objectStructure.Accept(gradeSection);
        }
    }


    public class Woman : IElement
    {
        public Woman(string name, int weight)
        {
            Name = name;
            Weight = weight;
        }
        public string Name { get; set; }
        /// <summary>
        /// 体重
        /// </summary>
        public int Weight { get; set; }

        public void Accept(IVisitor visitor)
        {
            visitor.Visit(this);
        }
    }
    public class Man : IElement
    {
        public Man(string name ,int height)
        {
            Name = name;
            Height = height;
        }
        public string Name { get; set; }

        /// <summary>
        /// 身高
        /// </summary>
        public int Height { get; set; }

        public void Accept(IVisitor visitor)
        {
            visitor.Visit(this);
        }
    }

    public interface IVisitor
    {
        void Visit(Woman woman);
        void Visit(Man man);
    }

    public class GradeSelection : IVisitor
    {
        public void Visit(Woman woman)
        {
            //重大于50kg
            if (woman.Weight > 50)
            {
                Console.WriteLine(woman.Name+ " 的身材不好");
            }
            else
            {
                Console.WriteLine(woman.Name+" is good");
            }
        }

        public void Visit(Man man)
        {
            //身高低于170cm
            if (man.Height < 170)
            {
                Console.WriteLine( man.Name+" 身材不好");
            }
            else
            {
                Console.WriteLine(man.Name+" is good");
            }
        }
    }

    public interface IElement
    {
        void Accept(IVisitor visitor);
    }

    /// <summary>
    /// 元素集合
    /// </summary>
    public class ObjectStructure:List<IElement>
    {
        public void Accept(IVisitor visitor)
        {
            ForEach(item => item.Accept(visitor));
        }
    }
}
